package com.imageim.yimirror.ui.fragment;

import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;

import com.imageim.yimirror.R;
import com.imageim.yimirror.base.BaseFragment;
import com.imageim.yimirror.ui.adapter.SlideSwitchFragmentAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Yuaihen.
 * on 2018/8/6
 * 智能分析Fragment （显示身线数值参考图）
 */
public class IntelligentAnalysisFragment extends BaseFragment {

    private static final String TAG = "IntelligentAnalysisFragment";
    @BindView(R.id.view_pager)
    ViewPager mViewPager;
    @BindView(R.id.linear_indicator)
    LinearLayout mLinearIndicator;
    private FragmentManager mFragmentManager;
    //上个page页面的positon
    private int mNum;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_intelligent_analysis;
    }

    @Override
    protected void initListener() {
        initPageChangeListener();
    }


    @Override
    protected void initData() {
        //Fragment嵌套Fragment需要使用getChildFragmentManager
        mFragmentManager = getChildFragmentManager();

        RadarFragmentForAnalyze radarFragment = new RadarFragmentForAnalyze();
        BodyLineWithPhotoFragment bodyLineWithPhotoFragment = new BodyLineWithPhotoFragment();
        List<BaseFragment> fragmentList = new ArrayList<>();
        fragmentList.add(radarFragment);
        fragmentList.add(bodyLineWithPhotoFragment);

        //创建底部指示器(小圆点)
        for (int i = 0; i < fragmentList.size(); i++) {
            View view = new View(mContext);
            view.setBackgroundResource(R.drawable.selector_indicator);
            view.setEnabled(false);
            //设置宽高
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(20, 20);
            //设置间隔
            if (i != 0) {
                layoutParams.leftMargin = 20;
            }
            //添加到LinearLayout
            mLinearIndicator.addView(view, layoutParams);
        }

        SlideSwitchFragmentAdapter adapter = new SlideSwitchFragmentAdapter(mFragmentManager, fragmentList);
        mViewPager.setAdapter(adapter);

        //第一次显示小白点
        if (mLinearIndicator.getChildCount() > 0) {
            mLinearIndicator.getChildAt(0).setEnabled(true);
        }
    }


    private void initPageChangeListener() {
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mLinearIndicator.getChildAt(mNum).setEnabled(false);
                mLinearIndicator.getChildAt(position).setEnabled(true);
                mNum = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

}







